﻿using System;
using System.Collections;
using System.Data.OleDb;
using System.Windows.Forms;

namespace Texpro
{
    public partial class FMAD103 : Form
    {

        //int i = 0;
        ArrayList data_values = new ArrayList();

        public FMAD103()
        {
            InitializeComponent();
        }

        private void frm_color_Load(object sender, EventArgs e)
        {
            load_form();
           // UseWaitCursor = false;
        }

        private void clear_form()
        {
            data_values.Clear();
            txt_color_id.Text = String.Empty;
            txt_color_name.Text = String.Empty;
        }

        private void load_form()
        {
            lbl_form_title.Text = this.Text;
            clear_form();
            switch (PCPUB.m_entry_form_mode)
            {
                case "ADDNEW":
                    txt_color_id.Enabled = true;
                    txt_color_id.Focus();
                    break;
                case "EDIT":
                    txt_color_id.Enabled = false;
                    fill_form((PCPUB.m_record_id));
                    txt_color_id.Focus();
                    break;
            }
        }

        private bool save_form()
        {
            bool b_return = false;
            try
            {
                data_values.Clear();
                data_values.Add(txt_color_id.Text.ToString());
                data_values.Add(txt_color_name.Text.ToString());
                switch (PCPUB.m_entry_form_mode)
                {
                    case "ADDNEW":
                        b_return = PCDB.insert_tis_color(data_values);
                        break;
                    case "EDIT":
                        b_return = PCDB.update_tis_color(data_values);
                        break;
                }
            }
            catch (Exception e)
            {
                b_return = false;
                PCMSG.ShowError(e);
            }
            return b_return;
        }

        private bool validate_form()
        {
            if (txt_color_id.Text == String.Empty)
            {
                PCMSG.ShowError("Yarn Color Id field should not be empty.");
                txt_color_id.Focus();
                return false;
            }
            else if (txt_color_name.Text == String.Empty)
            {
                PCMSG.ShowError("Yarn Color Name field should not be empty.");
                txt_color_name.Focus();
                return false;
            }
            return true;
        }

        private void but_save_Click(object sender, EventArgs e)
        {
            if (validate_form() == true)
            {
                if (save_form() == true)
                {
                    PCMSG.Show("Record saved successfully.");
                    clear_form();
                    PCPUB.m_admin_color_id = String.Empty;
                    switch (PCPUB.m_entry_form_mode)
                    {
                        case "ADDNEW":
                            txt_color_id.Focus();
                            break;
                        case "EDIT":
                            this.Close();
                           break;
                    }
                }
            }
        }

        private void but_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frm_yarn_count_KeyUp(object sender, KeyEventArgs e)
        {
            //if (e.KeyCode == Keys.Enter)
            //{
            //    e.Handled = false;
            //    SendKeys.Send("{TAB}");
            //}
        }

        private void fill_form(string p_id)
        {
            String strQuery = String.Empty;
            OleDbConnection sqlConn = new OleDbConnection();
            OleDbCommand sqlComm = new OleDbCommand();
            OleDbDataReader sqlRead;// = new OleDbDataReader();
            DateTime strReturn = DateTime.Now;
            //           
            sqlConn.ConnectionString = PCPUB.m_oledb_connection.ToString();
            sqlConn.Open();
            //
            strQuery = String.Empty;
            strQuery += " SELECT";
            strQuery += " [color_id],";
            strQuery += " [color_name]";
            strQuery += " FROM [tis_color]";
            strQuery += " WHERE [color_id] = '" + p_id + "'";
            //
            sqlComm.Connection = sqlConn;
            sqlComm.CommandText = strQuery.ToString();
            sqlRead = sqlComm.ExecuteReader();
            //
            if (sqlRead.Read())
            {
                txt_color_id.Text = sqlRead["color_id"].ToString();
                txt_color_name.Text = sqlRead["color_name"].ToString();
            }
            //
            sqlRead.Close();
            sqlConn.Close();
            sqlRead.Dispose();
            sqlComm.Dispose();
            sqlConn.Dispose();
        }

        private void text_id_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsLetterOrDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back))
            {
                e.Handled = true;
            }
        }

        private void text_gen_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsLetterOrDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Space || e.KeyChar == '.'))
            {
                e.Handled = true;
            }
        }

        private void text_int_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back))
            {
                e.Handled = true;
            }
        }

        private void text_dec_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == '.'))
            {
                e.Handled = true;
            }
        }

    }
}